package com.dnjn.mes.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.dnjn.mes.common.core.domain.AjaxResult;
import com.dnjn.mes.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.tags.Tag;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

import com.dnjn.mes.common.core.controller.BaseController;

import com.dnjn.mes.biz.service.IMesRecordMaintainService;
import com.dnjn.mes.biz.domain.MesRecordMaintain;

/**
 * <p>
 * 保养记录表 upkeep 前端控制器
 * </p>
 *
 * @author Amber
 * @since 2023-08-04
 */
@Tag(name = "MesRecordMaintain 保养记录表 upkeep", description = "保养记录表 upkeep")
@RestController
@RequestMapping("/biz/mesRecordMaintain")
public class MesRecordMaintainController extends BaseController {

    @Autowired
    protected IMesRecordMaintainService baseService;

    @GetMapping("/get/{id}")
    public AjaxResult get(@PathVariable Long id) {
        return success(baseService.getById(id));
    }

    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody MesRecordMaintain domain) {
        domain.setCreateBy(getUsername());
        return toAjax(baseService.save(domain));
    }

    @PutMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody MesRecordMaintain domain) {
        return toAjax(baseService.updateById(domain));
    }

    @DeleteMapping("/{pkIds}")
    public AjaxResult remove(@PathVariable Long[] pkIds) {
        baseService.removeBatchByIds(Arrays.asList(pkIds));
        return success();
    }

    @PostMapping("/page")
    public AjaxResult page(@RequestBody MesRecordMaintain domain, @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page page = Page.of(current, size);
        Page<MesRecordMaintain> list = baseService.page(page, Wrappers.query(domain));
        return success(list);
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response, MesRecordMaintain domain) {
        List<MesRecordMaintain> list = baseService.list(Wrappers.query(domain));
        ExcelUtil<MesRecordMaintain> util = new ExcelUtil<MesRecordMaintain>(MesRecordMaintain.class);
        util.exportExcel(response, list, "参数数据");
    }


    /**
     * 数据统计->保养记录
     */
    @Operation(summary = "保养记录")
    @PostMapping("/maintenanceLog")
    public AjaxResult maintenanceLog(@RequestParam(defaultValue = "1") Integer current,
                                     @RequestParam(defaultValue = "10") Integer size,
                                     @RequestParam(required = false) @Parameter(description = "workpieceName") String searchKey, @RequestHeader(value = "equipmentId") long equipmentId) {
        PageHelper.startPage(current, size);
        List<MesRecordMaintain> list = baseService.maintenanceLog(equipmentId,searchKey);
        PageInfo pageInfo = new PageInfo(list);
        return success(pageInfo);
    }

}